package com.emm.yixun.common.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;


/**
 * 
 * <p>WebUtil</p>
 */
public class WebUtil {
	
    private static HttpSession getSession(HttpServletRequest request) {
        return request.getSession();
    }
    
    public static void addSessionValue(HttpServletRequest request, String name, Object obj) {
        getSession(request).setAttribute(name, obj);
    }


    public static void removeSessionValue(HttpServletRequest request, String name) {
        getSession(request).removeAttribute(name);
    }

    public static Object getSessionValue(HttpServletRequest request, String name) {
        return getSession(request).getAttribute(name);
    }
    
	public static void saveUserToken(HttpServletRequest request,String userId){
		ServletContext servletContext=request.getServletContext();
		Map<String,String> userTokenMap=(Map<String,String>)servletContext.getAttribute("userTokenMap");
		if(null==userTokenMap){
			userTokenMap=new HashMap<String, String>();
		}
		//删除应用程序中有关userId的缓存
		for(String token : userTokenMap.keySet()){
			String[] tokenArray=token.split("_");
			if(tokenArray[0].equals(userId)){
				userTokenMap.remove(token);
			}
		}
		String sessionId=request.getSession().getId();
		String cacheKey=userId+"_"+sessionId;
		userTokenMap.put(cacheKey, cacheKey);
		servletContext.setAttribute("userTokenMap", userTokenMap);
	}
	
	public static boolean isToLogin(HttpServletRequest request,String userId){
		ServletContext servletContext=request.getServletContext();
		Map<String,String> userTokenMap=(Map<String,String>)servletContext.getAttribute("userTokenMap");
		if(null==userTokenMap){
			userTokenMap=new HashMap<String, String>();
		}
		String sessionId=request.getSession().getId();
		String userToken=userId+"_"+sessionId;
		for(String token : userTokenMap.keySet()){
			if(token.equals(userToken)){
				return false;
			}
		}
		return true;
	}




    /**
     * 
     *<p>判断是否为ajax请求<p>
     * @Title isAjax 
     * @param request
     * @return boolean    返回类型 
     */
    public static boolean isAjax(HttpServletRequest request) {
        boolean isAjax = false;
        String ajaxRequest = request.getHeader("x-Requested-With");
        if (ajaxRequest != null && ajaxRequest.equals("XMLHttpRequest")) {
            isAjax = true;
        }
        return isAjax;
    }

    /**
     * 取得客户端的IP
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("http_x_forwarded_for");
        }
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-forwarded-for");
        }
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
